/**
 * Created by Forcs on 16/4/6.
 */

import RouteService from './RouteService'

var Application = function (options) {
  var self = this
  self.$options = options
  self._app = null
  self._routeService = null
}

Application.prototype.run = function (app) {
  this._app = app
  this._routeService = new RouteService(app)
}

Application.prototype.toPage = function () {
  var route = {}
  var options = {}
  route.name = arguments[0]
  if (arguments.length > 1) {
    route.params = arguments[1]
    options.params = route.params
  }
  if (arguments.length > 2) {
    route.query = arguments[2]
    options.query = route.query
  }
  if (arguments.length > 3) {
    let arg = arguments[3]
    for (let key in arg) {
      options[key] = arg[key]
    }
  }
  this._routeService.goto(route.name, options)
  this._app.$router.go(route)
}

Application.prototype.closePage = function () {
  var self = this
  self._routeService.back()
  let current = self._routeService.current()
  var route = {
    name: current.name
  }
  var options = current.options
  if (options) {
    if (options['params']) {
      route.params = options['params']
    }
    if (options['query']) {
      route.query = options['query']
    }
  }
  this._app.$router.go(route)
}

Application.install = function (Vue, options) {
  Vue.prototype.$application = new Application(options)
}

export default Application
